<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="72px"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-form-item label="房型名称" prop="roomName">
            <el-input
              v-model="queryParams.roomName"
              placeholder="请输入房型名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item label="房型分类" prop="roomClass">
            <el-select
              v-model="queryParams.roomClass"
              placeholder="请选择房型分类"
              clearable
            >
              <el-option
                v-for="dict in room_category"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                v-for="dict in wdm_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:roomType:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:roomType:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:roomType:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:roomType:export']"
          >导出</el-button
        >
      </el-col>
      <!-- 1.导入 -->

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roomTypeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房型ID" align="center" prop="roomTypeId" />
      <el-table-column label="房型名称" align="center" prop="roomName" />
      <el-table-column label="英文名称" align="center" prop="englishName" />
      <el-table-column label="房型分类" align="center" prop="roomClass">
        <template #default="scope">
          <dict-tag :options="room_category" :value="scope.row.roomClass" />
        </template>
      </el-table-column>
      <el-table-column label="早餐类型" align="center" prop="breakfastType">
        <template #default="scope">
          <dict-tag :options="breakfa_type" :value="scope.row.breakfastType" />
        </template>
      </el-table-column>
      <el-table-column label="房型面积" align="center" prop="floorArea" />
      <el-table-column
        label="最大入住人数"
        align="center"
        prop="maxOccupancy"
      />
      <el-table-column label="起始楼层" align="center" prop="startFloor" />
      <el-table-column label="结束楼层" align="center" prop="endFloor" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:roomType:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:roomType:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 2.导入对话框 -->

    <!-- 添加或修改房型管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="roomTypeRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="房型名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房型名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="房型分类" prop="roomClass">
          <el-radio-group v-model="form.roomClass">
            <el-radio
              v-for="dict in room_category"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="早餐类型" prop="breakfastType">
          <el-select v-model="form.breakfastType" placeholder="请选择早餐类型">
            <el-option
              v-for="dict in breakfa_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房型面积" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入房型面积" />
        </el-form-item>
        <el-form-item label="最大入住人数" prop="maxOccupancy">
          <el-input
            v-model="form.maxOccupancy"
            placeholder="请输入最大入住人数"
          />
        </el-form-item>
        <el-form-item label="起始楼层" prop="startFloor">
          <el-input v-model="form.startFloor" placeholder="请输入起始楼层" />
        </el-form-item>
        <el-form-item label="结束楼层" prop="endFloor">
          <el-input v-model="form.endFloor" placeholder="请输入结束楼层" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in wdm_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in del_flag"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-divider content-position="center">床型配置信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddBedConfig"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="Delete"
              @click="handleDeleteBedConfig"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="bedConfigList"
          :row-class-name="rowBedConfigIndex"
          @selection-change="handleBedConfigSelectionChange"
          ref="bedConfig"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="房型ID" prop="roomTypeId" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.roomTypeId"
                placeholder="请输入房型ID"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="床型类别(0=大床,1=双床,2=榻榻米)"
            prop="bedType"
            width="150"
          >
            <template #default="scope">
              <el-select
                v-model="scope.row.bedType"
                placeholder="请选择床型类别(0=大床,1=双床,2=榻榻米)"
              >
                <el-option
                  v-for="dict in bed_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="床数量" prop="bedQuantity" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.bedQuantity"
                placeholder="请输入床数量"
              />
            </template>
          </el-table-column>
          <el-table-column label="床宽" prop="bedWidth" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.bedWidth" placeholder="请输入床宽" />
            </template>
          </el-table-column>
          <el-table-column label="床长" prop="bedLength" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.bedLength"
                placeholder="请输入床长"
              />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in wdm_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RoomType">
import {
  listRoomType,
  getRoomType,
  delRoomType,
  addRoomType,
  updateRoomType,
} from "@/api/manage/roomType";

const { proxy } = getCurrentInstance();
const { room_category, wdm_status, breakfa_type, bed_type, del_flag } =
  proxy.useDict(
    "room_category",
    "wdm_status",
    "breakfa_type",
    "bed_type",
    "del_flag"
  );

const roomTypeList = ref([]);
const bedConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedBedConfig = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: null,
    roomClass: null,
    status: null,
  },
  rules: {
    roomName: [
      { required: true, message: "房型名称不能为空", trigger: "blur" },
    ],
    roomClass: [
      { required: true, message: "房型分类不能为空", trigger: "change" },
    ],
  },
  // 3.导入参数
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房型管理列表 */
function getList() {
  loading.value = true;
  listRoomType(queryParams.value).then((response) => {
    roomTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    roomTypeId: null,
    roomName: null,
    englishName: null,
    roomClass: null,
    breakfastType: null,
    floorArea: null,
    maxOccupancy: null,
    startFloor: null,
    endFloor: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  bedConfigList.value = [];
  proxy.resetForm("roomTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.roomTypeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加房型管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _roomTypeId = row.roomTypeId || ids.value;
  getRoomType(_roomTypeId).then((response) => {
    form.value = response.data;
    bedConfigList.value = response.data.bedConfigList;
    open.value = true;
    title.value = "修改房型管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roomTypeRef"].validate((valid) => {
    if (valid) {
      form.value.bedConfigList = bedConfigList.value;
      if (form.value.roomTypeId != null) {
        updateRoomType(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRoomType(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _roomTypeIds = row.roomTypeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除房型管理编号为"' + _roomTypeIds + '"的数据项？')
    .then(function () {
      return delRoomType(_roomTypeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 床型配置序号 */
function rowBedConfigIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 床型配置添加按钮操作 */
function handleAddBedConfig() {
  let obj = {};
  obj.roomTypeId = "";
  obj.bedType = "";
  obj.bedQuantity = "";
  obj.bedWidth = "";
  obj.bedLength = "";
  obj.sortOrder = "";
  obj.status = "";
  obj.remark = "";
  bedConfigList.value.push(obj);
}

/** 床型配置删除按钮操作 */
function handleDeleteBedConfig() {
  if (checkedBedConfig.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的床型配置数据");
  } else {
    const bedConfigs = bedConfigList.value;
    const checkedBedConfigs = checkedBedConfig.value;
    bedConfigList.value = bedConfigs.filter(function (item) {
      return checkedBedConfigs.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleBedConfigSelectionChange(selection) {
  checkedBedConfig.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "manage/roomType/export",
    {
      ...queryParams.value,
    },
    `roomType_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
